#include <stdio.h>
#include <string>

struct std_string
{
	size_t length;	
	size_t capacity;
	size_t refcount;
};

void dump_std_string(std::string s)
{
	char *p1 = *(char **) &s;	/* gcc type check workaround */
	struct std_string *p2 = (struct std_string *)(p1 - sizeof(struct std_string));
	printf("[%s] size:%d capacity:%d\n", p1, p2->length, p2->capacity);
}

int main()
{
	std::string s1 = "short string";
	std::string s2 = "string longer that 16 bytes";

	dump_std_string(s1);
	dump_std_string(s2);

	// that works without using c_str()
	printf("%s\n", &s1);
	printf("%s\n", &s2);
}

#if 0
/*
 * intel
 */
0000000000001249 <_Z15dump_std_stringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE>:
    1249:	f3 0f 1e fa          	endbr64 
    124d:	55                   	push   %rbp
    124e:	48 89 e5             	mov    %rsp,%rbp
    1251:	48 83 ec 20          	sub    $0x20,%rsp
    1255:	48 89 7d e8          	mov    %rdi,-0x18(%rbp)
    1259:	48 8b 45 e8          	mov    -0x18(%rbp),%rax
    125d:	48 8b 00             	mov    (%rax),%rax
    1260:	48 89 45 f0          	mov    %rax,-0x10(%rbp)
    1264:	48 8b 45 f0          	mov    -0x10(%rbp),%rax
    1268:	48 83 e8 18          	sub    $0x18,%rax
    126c:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    1270:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    1274:	48 8b 48 08          	mov    0x8(%rax),%rcx
    1278:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    127c:	48 8b 10             	mov    (%rax),%rdx
    127f:	48 8b 45 f0          	mov    -0x10(%rbp),%rax
    1283:	48 89 c6             	mov    %rax,%rsi
    1286:	48 8d 3d 78 0d 00 00 	lea    0xd78(%rip),%rdi        # 2005 <_ZStL19piecewise_construct+0x1>
    128d:	b8 00 00 00 00       	mov    $0x0,%eax
    1292:	e8 39 fe ff ff       	callq  10d0 <printf@plt>
    1297:	90                   	nop
    1298:	c9                   	leaveq 
    1299:	c3                   	retq   

000000000000129a <main>:
    129a:	f3 0f 1e fa          	endbr64 
    129e:	55                   	push   %rbp
    129f:	48 89 e5             	mov    %rsp,%rbp
    12a2:	53                   	push   %rbx
    12a3:	48 81 ec 88 00 00 00 	sub    $0x88,%rsp
    12aa:	64 48 8b 04 25 28 00 	mov    %fs:0x28,%rax
    12b1:	00 00 
    12b3:	48 89 45 e8          	mov    %rax,-0x18(%rbp)
    12b7:	31 c0                	xor    %eax,%eax
    12b9:	48 8d 85 7f ff ff ff 	lea    -0x81(%rbp),%rax
    12c0:	48 89 c7             	mov    %rax,%rdi
    12c3:	e8 88 fe ff ff       	callq  1150 <_ZNSaIcEC1Ev@plt>
    12c8:	48 8d 95 7f ff ff ff 	lea    -0x81(%rbp),%rdx
    12cf:	48 8d 45 80          	lea    -0x80(%rbp),%rax
    12d3:	48 8d 35 45 0d 00 00 	lea    0xd45(%rip),%rsi        # 201f <_ZStL19piecewise_construct+0x1b>
    12da:	48 89 c7             	mov    %rax,%rdi
    12dd:	e8 3e fe ff ff       	callq  1120 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EPKcRKS3_@plt>
    12e2:	48 8d 85 7f ff ff ff 	lea    -0x81(%rbp),%rax
    12e9:	48 89 c7             	mov    %rax,%rdi
    12ec:	e8 0f fe ff ff       	callq  1100 <_ZNSaIcED1Ev@plt>
    12f1:	48 8d 85 7f ff ff ff 	lea    -0x81(%rbp),%rax
    12f8:	48 89 c7             	mov    %rax,%rdi
    12fb:	e8 50 fe ff ff       	callq  1150 <_ZNSaIcEC1Ev@plt>
    1300:	48 8d 95 7f ff ff ff 	lea    -0x81(%rbp),%rdx
    1307:	48 8d 45 a0          	lea    -0x60(%rbp),%rax
    130b:	48 8d 35 1a 0d 00 00 	lea    0xd1a(%rip),%rsi        # 202c <_ZStL19piecewise_construct+0x28>
    1312:	48 89 c7             	mov    %rax,%rdi
    1315:	e8 06 fe ff ff       	callq  1120 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EPKcRKS3_@plt>
    131a:	48 8d 85 7f ff ff ff 	lea    -0x81(%rbp),%rax
    1321:	48 89 c7             	mov    %rax,%rdi
    1324:	e8 d7 fd ff ff       	callq  1100 <_ZNSaIcED1Ev@plt>
    1329:	48 8d 55 80          	lea    -0x80(%rbp),%rdx
    132d:	48 8d 45 c0          	lea    -0x40(%rbp),%rax
    1331:	48 89 d6             	mov    %rdx,%rsi
    1334:	48 89 c7             	mov    %rax,%rdi
    1337:	e8 a4 fd ff ff       	callq  10e0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1ERKS4_@plt>
    133c:	48 8d 45 c0          	lea    -0x40(%rbp),%rax
    1340:	48 89 c7             	mov    %rax,%rdi
    1343:	e8 01 ff ff ff       	callq  1249 <_Z15dump_std_stringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE>
    1348:	48 8d 45 c0          	lea    -0x40(%rbp),%rax
    134c:	48 89 c7             	mov    %rax,%rdi
    134f:	e8 9c fd ff ff       	callq  10f0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
    1354:	48 8d 55 a0          	lea    -0x60(%rbp),%rdx
    1358:	48 8d 45 c0          	lea    -0x40(%rbp),%rax
    135c:	48 89 d6             	mov    %rdx,%rsi
    135f:	48 89 c7             	mov    %rax,%rdi
    1362:	e8 79 fd ff ff       	callq  10e0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1ERKS4_@plt>
    1367:	48 8d 45 c0          	lea    -0x40(%rbp),%rax
    136b:	48 89 c7             	mov    %rax,%rdi
    136e:	e8 d6 fe ff ff       	callq  1249 <_Z15dump_std_stringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE>
    1373:	48 8d 45 c0          	lea    -0x40(%rbp),%rax
    1377:	48 89 c7             	mov    %rax,%rdi
    137a:	e8 71 fd ff ff       	callq  10f0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
    137f:	48 8d 45 80          	lea    -0x80(%rbp),%rax
    1383:	48 89 c7             	mov    %rax,%rdi
    1386:	e8 a5 fd ff ff       	callq  1130 <puts@plt>
    138b:	48 8d 45 a0          	lea    -0x60(%rbp),%rax
    138f:	48 89 c7             	mov    %rax,%rdi
    1392:	e8 99 fd ff ff       	callq  1130 <puts@plt>
    1397:	48 8d 45 a0          	lea    -0x60(%rbp),%rax
    139b:	48 89 c7             	mov    %rax,%rdi
    139e:	e8 4d fd ff ff       	callq  10f0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
    13a3:	48 8d 45 80          	lea    -0x80(%rbp),%rax
    13a7:	48 89 c7             	mov    %rax,%rdi
    13aa:	e8 41 fd ff ff       	callq  10f0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
    13af:	b8 00 00 00 00       	mov    $0x0,%eax
    13b4:	48 8b 4d e8          	mov    -0x18(%rbp),%rcx
    13b8:	64 48 33 0c 25 28 00 	xor    %fs:0x28,%rcx
    13bf:	00 00 
    13c1:	0f 84 97 00 00 00    	je     145e <main+0x1c4>
    13c7:	e9 8d 00 00 00       	jmpq   1459 <main+0x1bf>
    13cc:	f3 0f 1e fa          	endbr64 
    13d0:	48 89 c3             	mov    %rax,%rbx
    13d3:	48 8d 85 7f ff ff ff 	lea    -0x81(%rbp),%rax
    13da:	48 89 c7             	mov    %rax,%rdi
    13dd:	e8 1e fd ff ff       	callq  1100 <_ZNSaIcED1Ev@plt>
    13e2:	48 89 d8             	mov    %rbx,%rax
    13e5:	48 89 c7             	mov    %rax,%rdi
    13e8:	e8 53 fd ff ff       	callq  1140 <_Unwind_Resume@plt>
    13ed:	f3 0f 1e fa          	endbr64 
    13f1:	48 89 c3             	mov    %rax,%rbx
    13f4:	48 8d 85 7f ff ff ff 	lea    -0x81(%rbp),%rax
    13fb:	48 89 c7             	mov    %rax,%rdi
    13fe:	e8 fd fc ff ff       	callq  1100 <_ZNSaIcED1Ev@plt>
    1403:	eb 3d                	jmp    1442 <main+0x1a8>
    1405:	f3 0f 1e fa          	endbr64 
    1409:	48 89 c3             	mov    %rax,%rbx
    140c:	48 8d 45 c0          	lea    -0x40(%rbp),%rax
    1410:	48 89 c7             	mov    %rax,%rdi
    1413:	e8 d8 fc ff ff       	callq  10f0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
    1418:	eb 1c                	jmp    1436 <main+0x19c>
    141a:	f3 0f 1e fa          	endbr64 
    141e:	48 89 c3             	mov    %rax,%rbx
    1421:	48 8d 45 c0          	lea    -0x40(%rbp),%rax
    1425:	48 89 c7             	mov    %rax,%rdi
    1428:	e8 c3 fc ff ff       	callq  10f0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
    142d:	eb 07                	jmp    1436 <main+0x19c>
    142f:	f3 0f 1e fa          	endbr64 
    1433:	48 89 c3             	mov    %rax,%rbx
    1436:	48 8d 45 a0          	lea    -0x60(%rbp),%rax
    143a:	48 89 c7             	mov    %rax,%rdi
    143d:	e8 ae fc ff ff       	callq  10f0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
    1442:	48 8d 45 80          	lea    -0x80(%rbp),%rax
    1446:	48 89 c7             	mov    %rax,%rdi
    1449:	e8 a2 fc ff ff       	callq  10f0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
    144e:	48 89 d8             	mov    %rbx,%rax
    1451:	48 89 c7             	mov    %rax,%rdi
    1454:	e8 e7 fc ff ff       	callq  1140 <_Unwind_Resume@plt>
    1459:	e8 b2 fc ff ff       	callq  1110 <__stack_chk_fail@plt>
    145e:	48 81 c4 88 00 00 00 	add    $0x88,%rsp
    1465:	5b                   	pop    %rbx
    1466:	5d                   	pop    %rbp
    1467:	c3                   	retq   
    1468:	0f 1f 84 00 00 00 00 	nopl   0x0(%rax,%rax,1)
    146f:	00 

/*
 * arm
 */
00000000004009ec <_Z15dump_std_stringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE>:
  4009ec:	a9bd7bfd 	stp	x29, x30, [sp, #-48]!
  4009f0:	910003fd 	mov	x29, sp
  4009f4:	f9000fa0 	str	x0, [x29, #24]
  4009f8:	f9400fa0 	ldr	x0, [x29, #24]
  4009fc:	f9400000 	ldr	x0, [x0]
  400a00:	f90017a0 	str	x0, [x29, #40]
  400a04:	f94017a0 	ldr	x0, [x29, #40]
  400a08:	d1006000 	sub	x0, x0, #0x18
  400a0c:	f90013a0 	str	x0, [x29, #32]
  400a10:	f94013a0 	ldr	x0, [x29, #32]
  400a14:	f9400001 	ldr	x1, [x0]
  400a18:	f94013a0 	ldr	x0, [x29, #32]
  400a1c:	f9400402 	ldr	x2, [x0, #8]
  400a20:	90000000 	adrp	x0, 400000 <_init-0x7f8>
  400a24:	91308000 	add	x0, x0, #0xc20
  400a28:	aa0203e3 	mov	x3, x2
  400a2c:	aa0103e2 	mov	x2, x1
  400a30:	f94017a1 	ldr	x1, [x29, #40]
  400a34:	97ffffa7 	bl	4008d0 <printf@plt>
  400a38:	d503201f 	nop
  400a3c:	a8c37bfd 	ldp	x29, x30, [sp], #48
  400a40:	d65f03c0 	ret

0000000000400a44 <main>:
  400a44:	a9b57bfd 	stp	x29, x30, [sp, #-176]!
  400a48:	910003fd 	mov	x29, sp
  400a4c:	f9000bf3 	str	x19, [sp, #16]
  400a50:	910183a0 	add	x0, x29, #0x60
  400a54:	97ffff97 	bl	4008b0 <_ZNSaIcEC1Ev@plt>
  400a58:	910183a2 	add	x2, x29, #0x60
  400a5c:	90000000 	adrp	x0, 400000 <_init-0x7f8>
  400a60:	91310001 	add	x1, x0, #0xc40
  400a64:	910103a0 	add	x0, x29, #0x40
  400a68:	97ffff8e 	bl	4008a0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EPKcRKS3_@plt>
  400a6c:	910183a0 	add	x0, x29, #0x60
  400a70:	97ffff80 	bl	400870 <_ZNSaIcED1Ev@plt>
  400a74:	9101a3a0 	add	x0, x29, #0x68
  400a78:	97ffff8e 	bl	4008b0 <_ZNSaIcEC1Ev@plt>
  400a7c:	9101a3a2 	add	x2, x29, #0x68
  400a80:	90000000 	adrp	x0, 400000 <_init-0x7f8>
  400a84:	91314001 	add	x1, x0, #0xc50
  400a88:	910083a0 	add	x0, x29, #0x20
  400a8c:	97ffff85 	bl	4008a0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EPKcRKS3_@plt>
  400a90:	9101a3a0 	add	x0, x29, #0x68
  400a94:	97ffff77 	bl	400870 <_ZNSaIcED1Ev@plt>
  400a98:	910103a1 	add	x1, x29, #0x40
  400a9c:	9101c3a0 	add	x0, x29, #0x70
  400aa0:	97ffff7c 	bl	400890 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1ERKS4_@plt>
  400aa4:	9101c3a0 	add	x0, x29, #0x70
  400aa8:	97ffffd1 	bl	4009ec <_Z15dump_std_stringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE>
  400aac:	9101c3a0 	add	x0, x29, #0x70
  400ab0:	97ffff68 	bl	400850 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
  400ab4:	910083a1 	add	x1, x29, #0x20
  400ab8:	910243a0 	add	x0, x29, #0x90
  400abc:	97ffff75 	bl	400890 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1ERKS4_@plt>
  400ac0:	910243a0 	add	x0, x29, #0x90
  400ac4:	97ffffca 	bl	4009ec <_Z15dump_std_stringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE>
  400ac8:	910243a0 	add	x0, x29, #0x90
  400acc:	97ffff61 	bl	400850 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
  400ad0:	910103a0 	add	x0, x29, #0x40
  400ad4:	97ffff6b 	bl	400880 <puts@plt>
  400ad8:	910083a0 	add	x0, x29, #0x20
  400adc:	97ffff69 	bl	400880 <puts@plt>
  400ae0:	910083a0 	add	x0, x29, #0x20
  400ae4:	97ffff5b 	bl	400850 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
  400ae8:	910103a0 	add	x0, x29, #0x40
  400aec:	97ffff59 	bl	400850 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
  400af0:	52800000 	mov	w0, #0x0                   	// #0
  400af4:	14000019 	b	400b58 <main+0x114>
  400af8:	aa0003f3 	mov	x19, x0
  400afc:	910183a0 	add	x0, x29, #0x60
  400b00:	97ffff5c 	bl	400870 <_ZNSaIcED1Ev@plt>
  400b04:	aa1303e0 	mov	x0, x19
  400b08:	97ffff76 	bl	4008e0 <_Unwind_Resume@plt>
  400b0c:	aa0003f3 	mov	x19, x0
  400b10:	9101a3a0 	add	x0, x29, #0x68
  400b14:	97ffff57 	bl	400870 <_ZNSaIcED1Ev@plt>
  400b18:	1400000c 	b	400b48 <main+0x104>
  400b1c:	aa0003f3 	mov	x19, x0
  400b20:	9101c3a0 	add	x0, x29, #0x70
  400b24:	97ffff4b 	bl	400850 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
  400b28:	14000006 	b	400b40 <main+0xfc>
  400b2c:	aa0003f3 	mov	x19, x0
  400b30:	910243a0 	add	x0, x29, #0x90
  400b34:	97ffff47 	bl	400850 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
  400b38:	14000002 	b	400b40 <main+0xfc>
  400b3c:	aa0003f3 	mov	x19, x0
  400b40:	910083a0 	add	x0, x29, #0x20
  400b44:	97ffff43 	bl	400850 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
  400b48:	910103a0 	add	x0, x29, #0x40
  400b4c:	97ffff41 	bl	400850 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
  400b50:	aa1303e0 	mov	x0, x19
  400b54:	97ffff63 	bl	4008e0 <_Unwind_Resume@plt>
  400b58:	f9400bf3 	ldr	x19, [sp, #16]
  400b5c:	a8cb7bfd 	ldp	x29, x30, [sp], #176
  400b60:	d65f03c0 	ret
  400b64:	00000000 	.inst	0x00000000 ; undefined


#endif

